package com.max.back.demo.api;

import cn.hutool.core.bean.BeanUtil;
import com.max.back.common.excel.PoiUtils;
import com.max.back.common.http.ResponseResult;
import com.max.back.common.sercurity.anno.AuthIgrone;
import com.max.back.demo.entity.Drug;
import com.max.back.demo.form.DrugForm;
import com.max.back.demo.form.DrugTypeDeleteForm;
import com.max.back.demo.service.DrugSer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
/**
 *药品控制器
 */
@CrossOrigin
@RestController
@RequestMapping("/api/drug")
public class DrugCtl {
    /**
     *药品服务接口
     */
    @Autowired
    private DrugSer ser;
    /**
     *excel工具包
     */
    private PoiUtils poiUtils;
    @PostMapping("/add")
    public ResponseResult<Boolean> add(@RequestBody @Validated({DrugForm.Base.class}) DrugForm form) {
        Drug drug = BeanUtil.toBean(form, Drug.class);
        boolean res = ser.checkAndInsert(drug);
        if (res) {
            return ResponseResult.getSuccessResult(true, "A006", null);
        } else {
            return ResponseResult.getMessageResult(false, "A007");
        }
    }
    @PostMapping("/delete")
    public ResponseResult<Boolean> delete(@RequestBody @Validated({DrugForm.Delete.class}) DrugForm form) {
        boolean res = ser.removeById(form.getNumber());
        if (res) {
            return ResponseResult.getSuccessResult(true, "A006", null);
        } else {
            return ResponseResult.getMessageResult(false, "A007");
        }
    }
    @PostMapping("/countdelete")
    public ResponseResult<Boolean> countdelete(@RequestBody @Valid DrugTypeDeleteForm form) {
        boolean res = false;
        for (String id : form.getIds()) {
            res = ser.removeById(id);
        }
        if (res) {
            return ResponseResult.getSuccessResult(true, "A006", null);
        } else {
            return ResponseResult.getMessageResult(false, "A007");
        }
    }
    @PostMapping("/update")
    public ResponseResult<Boolean> update(@RequestBody @Validated({DrugForm.Update.class}) DrugForm form) {
        Drug drug = BeanUtil.toBean(form, Drug.class);
        boolean res = ser.updateById(drug);
        if (res) {
            return ResponseResult.getSuccessResult(true, "A006", null);
        } else {
            return ResponseResult.getMessageResult(false, "A007");
        }
    }
    @PostMapping("/find")
    public ResponseResult<List<Drug>> find(@RequestBody @Validated({DrugForm.Find.class}) DrugForm form) {
        List<Drug> drugList = ser.findall(form.getNumber(), form.getType1(), form.getType2());
        return ResponseResult.getSuccessResult(drugList);
    }
    @AuthIgrone
    @PostMapping("/importdrug")
    public ResponseResult<Boolean> importData(MultipartFile file) throws IOException {
        // 1.自定义一个工具类拿到要解析的文件并解析成要存储的数据
        List<Drug> list = poiUtils.excel2Employee(file);
        // 2.遍历输出你解析的数据格式是否正确
        for (Drug employee : list) {
            System.out.println(employee.toString());
        }
        // 3.进行数据库添加操作
        boolean res = ser.saveBatch(list);
        if (res) {
            return ResponseResult.getSuccessResult(true, "A006", null);
        } else {
            return ResponseResult.getMessageResult(false, "A007");
        }
    }
}
